-- Copyright (C) 2013 wgm.china@gmail.com (王光明)
--vss.method为所有list方法的操作函数
--date:2013-12-11

local strfind = string.find
local strsub  = string.sub
local strbyte = string.byte
local strrep  = string.rep
local strchar = string.char
local strrep  = string.rep
local strlen  = string.len
local type    = type
local pairs   = pairs
local tonumber= tonumber 
local tb_insert = table.insert
local os	    = os
local ngx		= ngx
local tostring  = tostring


-------1定义公共组件(开始)---------------
local ssdb_utils           = require("vss.ssdb.utils")
local list_utils           = require("vss.list.utils")
local config_table 		  = ngx.shared.gcs_config  					--定义gcs_config缓存
-------1定义公共组件（结束）----------------

module(...)
local version = '1.2'

-------2加载配置文件（结束）----------------

strsplit = function(s, p)
    local rt= {}
    string.gsub(s, '[^'..p..']+', function(w) table.insert(rt,w) end )
    return rt
end

--return version 中保存的版本号
function get_version()
	return version
end

--把id加入队列
--为了业务需要数据同时加入两个队列，一个是实时队列，一个内存队列
--一个是每天的队列
function push(lr,list_name,id)
	--把id加入实时队列
	local res,err = ssdb_utils.push(lr,list_name,id)
	if not res then return nil,err end
	local res,err = list_utils.push(lr,list_name,id)
	if not res then return nil,err end
	return res,err
end

--从队列中取id
function pop(lr,list_name)
	local res,err = list_utils.pop(lr,list_name)
	if not res then return nil,err end
	local id = res
	
	ssdb_utils.zdel(list_name,id)
	
	return res,err
end

--取队列的总数,参数为队列名或队列名table
--已经完成，并测试成功
function list_count(list_name)
	if not list_name then
		return nil,"list_name is nil"
	end
	local res,err
	local tb_res = {}
	if type(list_name) == "string" then
		res,err = list_utils.count(list_name)
		return res,err
	elseif type(list_name) == "table" then
		for _,list in pairs(list_name) do
			res,err = list_utils.count(list)
			if not res then
				return nil,err
			else
				tb_res[list] = res
			end	
		end
		return tb_res,err
	else
		return nil,"list_name is fails"
	end	
end

--从缓存中取数据,参数为key或keys
--已经完成，并测试成功
--如果缓存内容为空则返回""
function get_data(data)
	local tb_data = {}
	if not data then
	 	return nil,"The data must be greater than or equal to 1"
	end	 
	if type(data) ~="table" then
		tb_insert(tb_data,data)
	else
		tb_data = data
	end
	local res,err = list_utils.get(tb_data)
	if not res then
		local res,err = ssdb_utils.get(tb_data)
		if not res then return nil,err end
	else
		return res,err
	end	
end

--从缓存中取数据,参数为key或keys
--已经完成，并测试成功
--如果缓存内容为空则返回0
function get_data1(data)
	local tb_data = {}
	if not data then
	 	return nil,"The data must be greater than or equal to 1"
	end	 
	if type(data) ~="table" then
		tb_insert(tb_data,data)
	else
		tb_data = data
	end
	local res,err = list_utils.get1(tb_data)
	if not res then
		local res,err = ssdb_utils.get1(tb_data)
		if not res then return nil,err end
	else
		return res,err
	end	
end

--把数据保存到缓存中,参数为key=value的table
--已经完成，并测试成功
function set_data(data)
	local tb_data = {}
	if type(data) ~="table" then
		return nil,"Parameter must be table format"
	else
		tb_data = data
	end
	local res,err = ssdb_utils.set(tb_data)
	if res then
		local res,err = list_utils.set(tb_data)
		if not res then
			 return nil,err
		else
			return res,err
		end
	else
		return nil,err
	end	
end

--把数据保存到缓存中,参数为key=value的table
--已经完成，并测试成功
function del_data(data)
	local tb_data = {}
	if type(data) ~="table" then
		return nil,"Parameter must be table format"
	else
		tb_data = data
	end
	local res,err = ssdb_utils.del(tb_data)
	if res then
		local res,err = list_utils.del(tb_data)
		if not res then
			 return nil,err
		else
			return res,err
		end
	else
		return nil,err
	end	
end

--把记数器增加指定值，参数为key=value的table
--已经完成，并测试成功
function incr_data(data)
	local tb_data = {}
	if type(data) ~="table" then
		return nil,"Parameter must be table format"
	else
		tb_data = data
	end
	local res,err = ssdb_utils.incr(tb_data)
	return res,err
end

function save_data(table,tb_data)
	if table == "1" then table = "upload" end
	if table == "2" then table = "success" end
	if table == "3" then table = "fails" end
	local list_name_today = table .."_"..tostring(os.date("%Y%m%d",ngx.time()))
	if type(tb_data) == "table" then
		local tb_res = {}
		for key,value in pairs(tb_data) do
			local res,err = ssdb_utils.hset(list_name_today,key,value)
			if not res then return nil,err end
			local res,err = ssdb_utils.hset(table,key,value)
			if not res then return nil,err end
			tb_res.key = res
		end
		return tb_res,err
	else
		return nil,"Parameter must be table format"
	end	
end
